﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Windows;
using System.Windows.Threading;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using BrainTechLLC.WPF;
using ImageAppViewModels;

namespace Stats.Plugin
{
    public class ThumbnailCollectionStatsViewModel : BaseNotifyPropertyChangedUI
    {
        DispatcherTimer _timer;
        protected IThumbnailImageCollectionHandler _thumbHandler;
        ThreadSafeCounter _dispatcherPosted = new ThreadSafeCounter();
        ThreadSafeCounter _dispatcherFinished = new ThreadSafeCounter();
        ThreadSafeCounter _dispatcherPriChanged = new ThreadSafeCounter();
        ThreadSafeCounter _dispatcherAborted = new ThreadSafeCounter();

        public ThumbnailCollectionStatsViewModel(IThumbnailImageCollectionHandler thumbHandler)
        {
            _thumbHandler = thumbHandler;
            _timer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 2, 300) };
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Start();
            Application.Current.Dispatcher.Hooks.OperationPosted += new DispatcherHookEventHandler(Hooks_OperationPosted);
            Application.Current.Dispatcher.Hooks.OperationCompleted += new DispatcherHookEventHandler(Hooks_OperationCompleted);
            Application.Current.Dispatcher.Hooks.OperationAborted += new DispatcherHookEventHandler(Hooks_OperationAborted);
            Application.Current.Dispatcher.Hooks.OperationPriorityChanged += new DispatcherHookEventHandler(Hooks_OperationPriorityChanged);
        }

        void Hooks_OperationPriorityChanged(object sender, DispatcherHookEventArgs e)
        {
            _dispatcherPriChanged.Increment();
        }

        void Hooks_OperationAborted(object sender, DispatcherHookEventArgs e)
        {
            _dispatcherAborted.Increment();
        }

        void Hooks_OperationCompleted(object sender, DispatcherHookEventArgs e)
        {
            _dispatcherFinished.Increment();
        }

        void Hooks_OperationPosted(object sender, DispatcherHookEventArgs e)
        {
            _dispatcherPosted.Increment();
        }

        public int ThumbnailControlsInListview
        {
            get { return _thumbHandler.ThumbCount; }
        }

        public int DispatcherPosted
        {
            get { return _dispatcherPosted.Count; }
        }

        public int DispatcherCompleted
        {
            get { return _dispatcherFinished.Count; }
        }

        public int DispatcherAborted
        {
            get { return _dispatcherAborted.Count; }
        }

        public int DispatcherChangePriority
        {
            get { return _dispatcherPriChanged.Count; }
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            try
            {
                OnPropertyChanged("ThumbnailControlsInListview");
                OnPropertyChanged("DispatcherChangePriority");
                OnPropertyChanged("DispatcherAborted");
                OnPropertyChanged("DispatcherCompleted");
                OnPropertyChanged("DispatcherPosted");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }
    }
}
